var helper=require("Helpers");

//数字方块单元脚本，处理单元的向下移动及初始化每个方块的数字
cc.Class({
    extends: cc.Component,

    properties: {
       type:1
    },

    initData:function(pos,speed,rect,minNum,maxNum,star,easy,game){
        this.node.position=pos;
        this._game=game;
        this._speed=speed;
        this._rect=rect;
        let starIndex=-1;
        let easyIndex=-1;
        if(star){
            starIndex=helper.getRandomInt(0,this.node.childrenCount-1);
        }
        if(easy){
            easyIndex=helper.getRandomInt(0,this.node.childrenCount-1);
        }
        for(let i=0;i<this.node.childrenCount;i++){
            let cur=helper.getRandomInt(minNum,maxNum);
            this.node.children[i].getComponent("cell").createNum(cur,minNum,maxNum,starIndex==i?1:0,
                easyIndex==i?1:0,game);
        }
    },

    removeAllCells:function(){
        for(let i=0;i<this.node.childrenCount;i++){
            this._game.addScores(this.node.children[i].getComponent("cell")._curNum);
            this._game.removeCell(this.node.children[i],this.node.children[i].color);
        }
    },

    move:function(dt){
        let nowY=this.node.y-this._speed*dt;
        this.node.position=cc.p(this.node.x,nowY);
        if(nowY<-this._rect.height/2-this.node.height/2-50){
           this.node.destroy();
        }
    },

    // LIFE-CYCLE CALLBACKS:
    update:function (dt) {
        this.move(dt);
    },
});
